package com.projectimport.controller;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.projectimport.service.ProjectImportService;

import junit.framework.Test;
import net.sf.mpxj.ProjectFile;
import net.sf.mpxj.mpp.MPPReader;

@Controller
public class ProjectImportController {

	final Logger logger=LoggerFactory.getLogger(this.getClass());

	@Resource
	private ProjectImportService ProjectImportService;


	@RequestMapping("/")
	public ModelAndView gotoindex() {
		logger.info("----访问主页----");
		ModelAndView modelAndView=new ModelAndView("/jsp/index");
		return modelAndView;
	}


	@RequestMapping("/gotofileupload")
	public ModelAndView gotofileupload(@RequestParam(required=true) String planversionSid,@RequestParam(required=true) String humanSid) {
		logger.info("----进入文件上传页面----");
		ModelAndView modelAndView=new ModelAndView("/jsp/fileupload");
		modelAndView.addObject("planVersionSid", planversionSid);
		modelAndView.addObject("humanSid", humanSid);
		return modelAndView;
	}

	@RequestMapping("/fileupload")
	public ModelAndView fileupload(@RequestParam(required=true) MultipartFile file,@RequestParam(required=true) String planVersionSid,@RequestParam(required=true) String humanSid) {
		ModelAndView modelAndView=new ModelAndView("/jsp/success");
		try {
			ProjectFile projectFile= new MPPReader().read(file.getInputStream());
			ProjectImportService.projectimport(projectFile, planVersionSid, humanSid);
		} catch (Exception e) {
			logger.error(e.toString());
			modelAndView.setViewName("/jsp/error");
		}
		return modelAndView;
	}
}
